package com.ari.xml;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class XMLFormatter {
    public static String format(String inputXML) throws TransformerException {
        Source xmlInput = null;
        StringWriter writer = new StringWriter();
        xmlInput = new StreamSource(new StringReader(inputXML));
        StreamResult xmlOutput = new StreamResult(writer);
        // Configure transformer
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        System.out.println(xmlOutput.getWriter().toString());
        return writer.toString();
    }

}
